<?php
/**
 * Created by PhpStorm.
 * User: M2
 * Date: 13-11-19
 * Time: 上午11:34
 */
class YlSqlContentsTaglib {
    
    private $_dbPrefix = 'yl_';
    
    public function __construct() {
        C(include SITE_PATH.'/extends/taglib/tagarr.php');
        $dbPrefix = C('db_prefix');
        !empty($dbPrefix) && $this->_dbPrefix = $dbPrefix;
    }
    public function parse($content) {
        if (stripos($content, 'yl:sqlcontents') !== false) {
            $matches = preg_parse($content, 'yl:sqlContents');
            foreach ($matches as $match) {
                $sqlContentsText = getParseContentText($match, 'yl:sqlContents');
                $stripslashes = getParseOneAttr($match, 'stripslashes');
                $querstring = preg_parse($sqlContentsText, 'yl:queryString');
                $sqlContentsText = str_replace($querstring[0], '', $sqlContentsText);
                $sqlContentsText = trim($sqlContentsText);
                $parseStr = '';
                $sql = getParseContentText($match, 'yl:queryString');
                $sql = trim($sql);
                
                //OOO add by jepin id去重复
                if(strpos($sql, $this->_dbPrefix.'cms_article')) {
                    $pos= strrpos($sql,'order by');
                    $posg= strrpos($sql,'group by');
                    if($pos>0&&$posg==0){
                        $str1=  substr($sql,0,$pos );
                        $strplus= pidsO();
                        $str2=  substr($sql,$pos);
                        $sql= $str1.$strplus.$str2;
                    }
                }
                //OOO  end
                //add next 2013-12-11 增加sql内容标签列表数据缓存
                if (($res = S('cache_sqlcontents_'.$sql)) === false) {
                    $res = M()->query($sql);
                    S('cache_sqlcontents_'.$sql, $res,600);
                    //OOO add by jepin id去重复 
                    if(strpos($sql, $this->_dbPrefix.'cms_article')) {
                     if(!empty($res)){
                     	$i=0;
                     	$newids= array();
                     	foreach ( $res as $v){
                     		if(!empty($v['id'])){
                     			$newids[$i] =$v['id'];
                     			$i++;
                     		}
                     	}
                     	pidsI($newids);
                     }
                    } 
                    //OOO  end 
                }
                if(empty($res)){
                	$text = taglib(C('yl:if'))->parse($sqlContentsText, $res);
                	$parseStr .= $text;
                }
                foreach ($res as $k => $v) {
                	//update next 2013-12-12 简化替换操作
                    $text = taglib(C('yl:if'))->parse($sqlContentsText, $v);
                    $text = taglib(C('yl:sqlcontent'))->parse($text,$v);
                    if(!empty($stripslashes)){
                        if(isset($v[$stripslashes]) && !empty($v[$stripslashes])){
                            $v[$stripslashes] = stripslashes($v[$stripslashes]);
                        }
                    }
                    if ((false !== strpos($text,'{')) &&  (false !== strpos($text,'}'))) {
                        preg_match_all('/\{Sql.(.*?)\}/eis',$text,$out);
                        for ($i=0; $i< count($out[1]); $i++) {
                            $text = str_ireplace($out[0][$i],$v[$out[1][$i]],$text);
                        }
                    }
                    //匹配img标签
                    $text = taglib(C('yl:image'))->parse($text,$v);
                    $parseStr .= $text;
                }
                $content = str_replace($match, $parseStr, $content);
            }
        }
        return $content;
    }
}